//
//  MainViewController.m
//  NTBuildOC
//
//  Created by Marcus Yu on 2022/1/14.
//

#import "MainViewController.h"


@import Masonry;

@interface MainViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *cardTextView;
@property (nonatomic, strong) MASConstraint *heightConstraint;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.cardTextView];
    
    __weak typeof(self) weakself = self;
    [self.cardTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.mas_equalTo(@300);
        weakself.heightConstraint = make.height.mas_greaterThanOrEqualTo(10);
    }];
    
//    [self textViewDidChange:self.cardTextView];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.cardTextView becomeFirstResponder];
    self.cardTextView.text = @"";
}


#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    NSLog(@"-----%@",textView.text);
    
    CGFloat height = [self calculateTextViewHeight:textView];
 
    self.heightConstraint.mas_greaterThanOrEqualTo(height);
    
}

- (CGFloat)calculateTextViewHeight:(UITextView *)textView {
    CGSize constraintSize = CGSizeMake(textView.frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    return size.height;
}





-(UITextView *)cardTextView {
    if (_cardTextView == nil) {
        _cardTextView = [[UITextView alloc]init];
        _cardTextView.layer.borderWidth = 1.0;
        _cardTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _cardTextView.font = [UIFont systemFontOfSize:20];
        _cardTextView.text = @"hello";
        _cardTextView.delegate = self;
        _cardTextView.scrollEnabled = NO;
        _cardTextView.textAlignment = NSTextAlignmentCenter;
    }
    return _cardTextView;
}


@end
